package com.sengled.cloud.biz.union.mgt.dao.sys.provider;

import com.sengled.cloud.biz.union.mgt.common.PageUtil;
import com.sengled.cloud.biz.union.mgt.model.po.Role;
import org.apache.commons.lang.StringUtils;

import java.util.Map;

/**
 * Created by csolo on 2017/11/8.
 */
public class RoleDaoProvider {

    public String updateRoleSQL(Role role) {
        StringBuilder sb = new StringBuilder();
        sb.append("UPDATE role set ");
        StringBuilder setStr = new StringBuilder();
        if(role.getRoleCode() != null){
            setStr.append(" role_code = #{roleCode}, ");
        }
        if(role.getRoleName() != null){
            setStr.append(" role_name = #{roleName}, ");
        }
        if(role.getRoleType() != null){
            setStr.append(" role_type = #{roleType}, ");
        }
        if(role.getCompanyId() != null){
            setStr.append(" company_id = #{companyId}, ");
        }
        if(role.getCreateDate() != null){
            setStr.append(" create_date = #{createDate}, ");
        }
        if(role.getOperatorId() != null){
            setStr.append(" operator_id = #{operatorId}, ");
        }
        if(role.getSealMark() != null){
            setStr.append(" seal_mark = #{sealMark}, ");
        }
        if(role.getEnableMark() != null){
            setStr.append(" enable_mark = #{enableMark}, ");
        }
        setStr.deleteCharAt(setStr.length() - 2);
        sb.append(setStr);
        sb.append(" where id = #{id} ");
        System.out.println("sql : ---------------->"+sb.toString());
        return sb.toString();
    }

    public String queryRoleSQL(Map map) {
        Role role = (Role)map.get("role");
        PageUtil pageUtil = (PageUtil)map.get("pageUtil");

        StringBuilder sb = new StringBuilder();
        sb.append("select id as id,role_name as roleName,role_code as roleCode,role_type as roleType" +
                ",company_id as companyId,create_date as createDate,operator_id as operatorId,seal_mark as sealMark" +
                ",enable_mark as enableMark from role where seal_mark = 0 and int001 is null ");
        StringBuilder setStr = new StringBuilder();
        if(role.getId() != null){
            setStr.append(" and id = #{role.id} ");
        }
        if(StringUtils.isNotEmpty(role.getRoleCode())){
            setStr.append(" and role_code like CONCAT('%', #{role.roleCode}, '%') ");
        }
        if(StringUtils.isNotEmpty(role.getRoleName())){
            setStr.append(" and role_name like CONCAT('%', #{role.roleName}, '%') ");
        }
        if(role.getRoleType() != null && role.getRoleType() != 0){
            setStr.append(" and role_type like CONCAT('%', #{role.roleType}, '%') ");
        }
        if(role.getCompanyId() != null ){
            setStr.append(" and company_id = #{role.companyId} ");
        }
        if(role.getCreateDate() != null){
            setStr.append(" and create_date = #{role.createDate} ");
        }
        if(role.getOperatorId() != null){
            setStr.append(" and operator_id = #{role.operatorId} ");
        }
        /*if(role.getSealMark() != null){
            setStr.append(" and seal_mark = #{role.sealMark} ");
        }*/
        if(role.getEnableMark() != null){
            setStr.append(" and enable_mark = #{role.enableMark} ");
        }
        setStr.append(" order by role_type desc,create_date desc limit #{pageUtil.start},#{pageUtil.pageSize}");
        sb.append(setStr);
        System.out.println("sql : ---------------->"+sb.toString());
        return sb.toString();
    }

    public String queryRoleCountSQL(Map map) {
        Role role = (Role)map.get("role");
        StringBuilder sb = new StringBuilder();
        sb.append("select count(id) from role where seal_mark = 0 and int001 is null ");
        StringBuilder setStr = new StringBuilder();
        if(role.getId() != null){
            setStr.append(" and id = #{role.id} ");
        }
        if(StringUtils.isNotEmpty(role.getRoleCode())){
            setStr.append(" and role_code like CONCAT('%', #{role.roleCode}, '%') ");
        }
        if(StringUtils.isNotEmpty(role.getRoleName())){
            setStr.append(" and role_name like CONCAT('%', #{role.roleName}, '%') ");
        }
        if(role.getRoleType() != null && role.getRoleType() != 0){
            setStr.append(" and role_type like CONCAT('%', #{role.roleType}, '%') ");
        }
        if(role.getCompanyId() != null ){
            setStr.append(" and company_id = #{role.companyId} ");
        }
        if(role.getCreateDate() != null){
            setStr.append(" and create_date = #{role.createDate} ");
        }
        if(role.getOperatorId() != null){
            setStr.append(" and operator_id = #{role.operatorId} ");
        }
        /*if(role.getSealMark() != null){
            setStr.append(" and seal_mark = #{role.sealMark} ");
        }*/
        if(role.getEnableMark() != null){
            setStr.append(" and enable_mark = #{role.enableMark} ");
        }
        setStr.append(" order by role_type desc,create_date desc ");
        sb.append(setStr);
        System.out.println("sql : ---------------->"+sb.toString());
        return sb.toString();
    }

}
